//
//  SearchAllViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SearchAllViewController.h"
#import "CustomerNetworking.h"
#import "Songer.h"
#import "SearchAllTitleView.h"
#import "MVDetailerModeL.h"
#import <UIImageView+WebCache.h>
#import "PlayerViewController.h"
#import "BaseClassNavigationController.h"
#import "AVPlayerViewController.h"
@interface SearchAllViewController ()<UITableViewDataSource, UITableViewDelegate, SearchChangeCell>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *songListArray;
@property (nonatomic, assign) NSInteger cellNum;
@property (nonatomic, retain) NSMutableArray *MVarray;
@end

@implementation SearchAllViewController
- (void)dealloc
{
    [_str release];
    [_tableView release];
    [_songListArray release];
    [_MVarray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.cellNum = 0;
    // 创建navgation
    [self creatNav];
    // 创建tableView
    [self creatTableView];
    // 获取数据
    [self getData];
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 5, 375, 600) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (1 == self.cellNum)
    {
        return self.MVarray.count;
    }
    return self.songListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == self.cellNum)
    {
        NSString *cellIdentM = @"cellM";
        UITableViewCell *cellM = [tableView dequeueReusableCellWithIdentifier:cellIdentM];
        if (nil == cellM)
        {
            cellM = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentM] autorelease];
            cellM.backgroundColor = [UIColor clearColor];
            cellM.selectionStyle = UITableViewCellSelectionStyleNone;
            cellM.textLabel.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
            cellM.detailTextLabel.textColor = [UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0];
        }
        cellM.textLabel.text = [self.MVarray[indexPath.row] videoName];
        cellM.detailTextLabel.text = [self.MVarray[indexPath.row] singerName];
        MVDetailerModeL *mv = self.MVarray[indexPath.row];
        [cellM.imageView sd_setImageWithURLStr:mv.picUrl];
        return cellM;
    }
    NSString *cellIdent = @"cellS";
    UITableViewCell *cellS = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (nil == cellS)
    {
        cellS = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdent] autorelease];
        cellS.backgroundColor = [UIColor clearColor];
        cellS.selectionStyle = UITableViewCellSelectionStyleNone;
        cellS.textLabel.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
        cellS.detailTextLabel.textColor = [UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0];
    }
    cellS.textLabel.text = [self.songListArray[indexPath.row] name];
    cellS.detailTextLabel.text = [self.songListArray[indexPath.row] singerName];
    return cellS;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == self.cellNum)
    {
        return 70;
    } else {
        return 50;
    }
}
- (void)creatNav
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"backGroud" ofType:@"jpg"]]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(barLeft)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:1.0];
    SearchAllTitleView *searchView = [[SearchAllTitleView alloc] initWithFrame:CGRectMake(0, 10, 80, 40)];
    searchView.delegate = self;
    self.navigationItem.titleView = searchView;
    [searchView release];
    
}
- (void)getData
{
    self.songListArray = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"%@", self.str];
    NSString *url = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.dongting.com/misc/search/song?q=%@&page=1&size=50&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=c61e1dfaea64217f93ecee8f89d3167d08db491c&idfa=7E083EC9-D67D-4492-8AD9-7AEE22796941&utdid=VhH%%2FqR36qsADAM70Q%%2FnVNw0E&alf=700606&bundle_id=com.ttpod.music&latitude=38.88245&longtitude=121.5399", url] andParameters:nil andBlock:^(id response) {
        NSArray *arr = [response objectForKey:@"data"];
        for (NSDictionary *dic in arr)
        {
            Songer *song = [Songer new];
            [song setValuesForKeysWithDictionary:dic];
            if (song.auditionList.count != 0)
            {
                [self.songListArray addObject:song];
            }
            [song release];
        }
        [self.tableView reloadData];
    }];
    
    self.MVarray = [NSMutableArray array];
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://so.ard.iyyin.com/s/video?q=%@&page=1&size=20&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=c61e1dfaea64217f93ecee8f89d3167d08db491c&idfa=7E083EC9-D67D-4492-8AD9-7AEE22796941&utdid=VhH%%2FqR36qsADAM70Q%%2FnVNw0E&alf=700606&bundle_id=com.ttpod.music&latitude=38.88245&longtitude=121.5399", url] andParameters:nil andBlock:^(id response) {
        NSArray *arr = [response objectForKey:@"data"];
        for (NSDictionary *dic in arr)
        {
            MVDetailerModeL *mv = [MVDetailerModeL new];
            [mv setValuesForKeysWithDictionary:dic];
            [self.MVarray addObject:mv];
            [mv release];
        }
        [self.tableView reloadData];
    }];
}
- (void)barLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    if (0 == self.cellNum)
    {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSInteger i = 0; i < self.songListArray.count; i++)
        {
            Songer *song = self.songListArray[i];
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setValue:song.albumName forKey:@"album"];// 专辑
            [dic setValue:song.name forKey:@"name"]; // 歌名
            [dic setValue:song.auditionList forKey:@"music"]; // 歌曲,model用musicType
            [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
            [dic setValue:song.albumId forKey:@"id"]; // id
            [arr addObject:dic];
        }
        player.musicArr = arr;
        [player MusicStart:indexPath.row];
    } else {
        AVPlayerViewController *AVplayer = [AVPlayerViewController new];
        BaseClassNavigationController *navgationController = [[BaseClassNavigationController alloc] initWithRootViewController:AVplayer];
        [navgationController.navigationBar setBackgroundImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"nav" ofType:@"jpg"]] forBarMetrics:UIBarMetricsCompact];
        [self presentViewController:navgationController animated:YES completion:^{
            
        }];
        AVplayer.urlString = [[self.MVarray[indexPath.row] mvList].firstObject url];
        AVplayer.idString = [self.MVarray[indexPath.row] tempId];
        [navgationController release];
        [AVplayer release];
    }
}
- (void)buttonChangeCellWithNum:(NSInteger)num
{
    self.cellNum = num;
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
